import datetime

from django.db import models
from django.contrib.auth.models import User

class TournamentType(models.Model):
   class Admin:
      pass

   name = models.CharField(maxlength=256)
   description = models.TextField()

class TimerStructure(models.Model):
   class Admin:
      pass

   name = models.CharField(maxlength=256)
   description = models.TextField()

class TimerLevel(models.Model):
   class Admin:
      pass

   structure = models.ForeignKey(TimerStructure)
   position = models.IntegerField()
   seconds = models.IntegerField()

class BlindStructure(models.Model):
   class Admin:
      pass

   name = models.CharField(maxlength=256)
   description = models.TextField()
   # BlindLevels

class BlindLevel(models.Model):
   class Admin:
      pass

   structure = models.ForeignKey(BlindStructure)
   position = models.IntegerField()
   small_blind = models.IntegerField()
   big_blind = models.IntegerField()
   time = models.IntegerField()

class Currency(models.Model):
   class Admin:
      pass

   name = models.CharField(maxlength=256) # Cash (US), Credit, ...
   symbol_pre = models.CharField(maxlength=16, default="$", blank=True) # $
   symbol_post = models.CharField(maxlength=16, default="", blank=True) # JPY

### Payout related

class PayoutStructure(models.Model):
   class Admin:
      pass

   name = models.CharField(maxlength=32)
   allocation = models.TextField()


class PayoutRow(models.Model):
   class Admin:
      pass

   players = models.IntegerField()
   structure = models.ForeignKey(PayoutStructure)


class PayoutSlot(models.Model):
   class Admin:
      pass

   row = models.ForeignKey(PayoutRow)
   place = models.IntegerField()
   units = models.IntegerField()

class Tournament(models.Model):
   class Admin:
      pass

   host = models.ForeignKey(User) # the tournament director
   title = models.CharField(maxlength=256) # "PokerDB Poker Classic"
   status = models.CharField(maxlength=128) # proposed, completed, deleted, ...
   start_date = models.DateTimeField('start date', default=datetime.datetime.now)
   end_date= models.DateTimeField('end date', default=datetime.datetime.now)
   game_type = models.ForeignKey(TournamentType, related_name='game_tourneys') # NL Texas Holdem, ...
   tournament_type = models.ForeignKey(TournamentType, related_name='type_tourneys') # Cash Tourney, ...
   buyin_amount = models.FloatField(max_digits=10, decimal_places=2)
   buyin_currency = models.ForeignKey(Currency)
   buyin_chips = models.IntegerField()
   rebuy_amount = models.FloatField(max_digits=10, decimal_places=2)
   timer_structure = models.ForeignKey(TimerStructure)
   blind_structure = models.ForeignKey(BlindStructure)
   main_payouts = models.ForeignKey(PayoutStructure, related_name='main_tourneys')
   rebuy_payouts = models.ForeignKey(PayoutStructure, related_name='rebuy_tourneys')

class TournamentTicket(models.Model):
   class Admin:
      pass

   tourney = models.ForeignKey(Tournament)
   player = models.ForeignKey(User)

class TournamentResult(models.Model):
   """ A result record for a particular tournamnet and user """
   class Admin:
      pass

   ticket = models.ForeignKey(TournamentTicket)
   status = models.CharField(maxlength=64) # active, out
   knockout_player = models.ForeignKey(User)
   knockout_date = models.DateTimeField('knockout time', default=datetime.datetime.now)
   knockout_position = models.IntegerField()

class TournamentEvent(models.Model):
   """ Sequence of messages saved with the tournament. """
   class Admin:
      pass

   tourney = models.ForeignKey(Tournament)
   event_date = models.DateTimeField('event time', default=datetime.datetime.now)
   name = models.CharField(maxlength=256)
   details = models.TextField()


class PaymentRecord(models.Model):
   class Admin:
      pass

   ticket = models.ForeignKey(TournamentTicket)
   payment_type = models.CharField(maxlength=64) # buyin, cashout
   payment_date = models.DateTimeField('record date', default=datetime.datetime.now)
   amount = models.FloatField(max_digits=10, decimal_places=2)
   currency = models.ForeignKey(Currency)

